﻿@using Stardust.Data;
@using Stardust.Data.Configs;
@using Stardust.Data.Deployment;
@using Stardust.Data.Monitors;
@{
    var path = Context.Request.Path + "";

    var flag = false;
    var appId = Context.Request.Query["appId"].ToInt(0);
    if (appId == 0 && path.EqualIgnoreCase("/Registry/App", "/Registry/App/Detail", "/Registry/App/Edit"))
    {
        appId = Context.Request.Query["Id"].ToInt(0);
        flag = true;
    }
    if (appId == 0 && path.EqualIgnoreCase("/Configs/AppConfig", "/Configs/AppConfig/Detail", "/Configs/AppConfig/Edit"))
    {
        var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppConfig.FindById(id);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (appId == 0 && path.EqualIgnoreCase("/Deployment/AppDeploy", "/Deployment/AppDeploy/Detail", "/Deployment/AppDeploy/Edit"))
    {
        var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppDeploy.FindById(id);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (appId == 0 && path.EqualIgnoreCase("/Monitors/AppTracer", "/Monitors/AppTracer/Detail", "/Monitors/AppTracer/Edit"))
    {
        var id = Context.Request.Query["Id"].ToInt(0);
        var entity = AppTracer.FindByID(id);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (path.StartsWithIgnoreCase("/Configs/ConfigData"))
    {
        var entity = AppConfig.FindById(appId);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (path.StartsWithIgnoreCase("/Deployment/AppDeployVersion", "/Deployment/AppDeployNode"))
    {
        var entity = AppDeploy.FindById(appId);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }
    if (path.StartsWithIgnoreCase("/Monitors/AppDayStat"))
    {
        var entity = AppTracer.FindByID(appId);
        if (entity != null && entity.AppId > 0) appId = entity.AppId;
    }

    var start = Context.Request.Query["dtStart"].ToDateTime();
    var end = Context.Request.Query["dtEnd"].ToDateTime();
    if (end.Year < 2000) end = Context.Request.Query["dtEnd2"].ToDateTime().AddSeconds(1);

    var dic = new Dictionary<String, Object>();
    dic["实例"] = "/Registry/AppOnline?appId=" + appId;
    dic["历史"] = "/Registry/AppHistory?appId=" + appId;
    dic["命令"] = "/Registry/AppCommand?appId=" + appId;
    dic["性能"] = "/Registry/AppMeter?appId=" + appId;
    dic["注册中心"] = "/Registry/AppService?appId=" + appId;
    dic["服务消费"] = "/Registry/AppConsume?appId=" + appId;

    var configs = AppConfig.FindAllByAppId(appId);
    dic["配置中心"] = "/Configs/AppConfig?appId=" + appId;
    if (configs.Count == 1)
    {
        dic["配置管理"] = "/Configs/ConfigData?appId=" + configs[0].Id;
    }

    var deploys = AppDeploy.FindAllByAppId(appId);
    dic["发布中心"] = "/Deployment/AppDeploy?appId=" + appId;
    if (deploys.Count == 1)
    {
        dic["版本上传"] = "/Deployment/AppDeployVersion?appId=" + deploys[0].Id;
        dic["发布节点"] = "/Deployment/AppDeployNode?&enable=1&appId=" + deploys[0].Id;
    }

    var monitors = AppTracer.FindAllByAppId(appId);
    dic["监控中心"] = "/Monitors/AppTracer?appId=" + appId;
    if (monitors.Count == 1)
    {
        dic["监控大屏"] = "/Monitors/AppDayStat?appId=" + monitors[0].ID;
    }

    // var dic2 = new Dictionary<String, Object>();
    // dic2["报警事件"] = "/Safety/AlarmEvent?appId=" + appId;
    // dic2["ADAS"] = "/Safety/ADASAlarm?appId=" + appId;
    // dic2["DSM"] = "/Safety/DSMAlarm?appId=" + appId;
    // dic2["BSD"] = "/Safety/BSDAlarm?appId=" + appId;
    // dic["报警"] = dic2;
}
@if (appId > 0)
{
    var app = App.FindById(appId);

    <div class="navbar-collapse collapse text-center">
        <ul class="nav nav-pills" style="margin-bottom: 10px; display: inline-block;float: none;">
            <li role="presentation" class="@(flag?"active":"")">
                <a href="/Registry/App/Edit?Id=@appId">应用：@(app?.ToString() ?? "信息")</a>
            </li>
            @foreach (var item in dic)
            {
                if (item.Value is IDictionary<String, Object> childs)
                {
                    <li role="presentation" class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">@item.Key <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            @foreach (var elm in childs)
                            {
                                var url = elm.Value + "";
                                var v = url.Substring(null, "?");
                                if (start.Year > 2000) url += "&dtStart=" + start.ToFullString();
                                if (end.Year > 2000) url += "&dtEnd=" + end.ToFullString();
                                <li role="presentation" class="@(path.EqualIgnoreCase(v)?"active":"")">
                                    <a href="@url">@elm.Key</a>
                                </li>
                            }
                        </ul>
                    </li>
                }
                else
                {
                    var url = item.Value + "";
                    var v = url.Substring(null, "?");
                    if (start.Year > 2000) url += "&dtStart=" + start.ToFullString();
                    if (end.Year > 2000) url += "&dtEnd=" + end.ToFullString();
                    <li role="presentation" class="@(path.EqualIgnoreCase(v)?"active":"")">
                        <a href="@url">@item.Key</a>
                    </li>
                }
            }
        </ul>
    </div>
}